package com.songchaojie.controller;


import com.github.pagehelper.PageInfo;
import com.songchaojie.entity.City;
import com.songchaojie.entity.Factory;
import com.songchaojie.entity.Supermarket;
import com.songchaojie.service.SupermarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SupermarketController {

    @Autowired
    SupermarketService supermarketService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("list")
    public String list(Supermarket supermarket, Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<Supermarket> pg = supermarketService.list(supermarket, pageNum, pageSize);
        model.addAttribute("pg",pg);
        model.addAttribute("su",supermarket);
        return "list";
    }
    @ResponseBody
    @RequestMapping("listCity")
    public List<City>  listCity(Integer pid){

        return supermarketService.listCity(pid);
    }
    @ResponseBody
    @RequestMapping("listFactory")
    public List<Factory>  listFactory(){
        if(!redisTemplate.hasKey("listFactory")){
            List<Factory> factoryList = supermarketService.listFactory();
            redisTemplate.opsForValue().set("listFactory",factoryList);
        }else{

        }
        return supermarketService.listFactory();
    }
}
